package com.mocha.handlers;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.mocha.bean.Employee;
import com.mocha.dao.DepartmentDao;
import com.mocha.dao.EmployeeDao;

@Controller
public class EmployHandler {

    @Autowired
    private EmployeeDao employDao;
    
    @Autowired
    private DepartmentDao departmentDao;
    
    @ModelAttribute
    public void getEmployee(@RequestParam(value = "id",required = false)Integer id,
                            Map<String, Object> map) {
        if(id != null) {
            map.put("employee",employDao.get(id));
        }
    }
    
    @RequestMapping(value = "/emp", method = RequestMethod.PUT )
    public String update(Employee employee) {
        employDao.save(employee);
        return "redirect:/emps";
    }
    
    @RequestMapping(value = "/emp/{id}",method = RequestMethod.GET)
    public String input(@PathVariable("id") Integer id, Map<String, Object> map) {
        map.put("employee", employDao.get(id));
        map.put("departments", departmentDao.getDepartments());
        return "input";
    }
    
    @RequestMapping(value = "/emp/{id}",method = RequestMethod.DELETE)
    public String delete(@PathVariable("id") Integer id) {
        employDao.delete(id);
        return "redirect:/emps";
    }
    
    @RequestMapping(value = "/emp",method = RequestMethod.POST)
    public String save(Employee employee) {
        employDao.save(employee);
        return "redirect:/emps";
    }
    
    @RequestMapping(value = "emp",method = RequestMethod.GET)
    public String input(Map<String, Object> map ) {
        map.put("departments", departmentDao.getDepartments());
        map.put("employee", new Employee());
        return "input";
    }
    
    @RequestMapping("/emps")
    public String list(Map<String, Object> map) {
        map.put("employees", employDao.getAll());
        return "list";
    }
    
    
}
